//
//  StoreTests.swift
//  FearfulNotificationTests
//
//  Created by 筱鹏 on 2019/5/24.
//  Copyright © 2019 筱鹏. All rights reserved.
//

import XCTest
@testable import FearfulNotification

class StoreTests: XCTestCase {

    override func setUp() {
        // Put setup code here. This method is called before the invocation of each test method in the class.
    }

    override func tearDown() {
        // Put teardown code here. This method is called after the invocation of each test method in the class.
    }
    
    func insertAnyValue(count: Int, finishClosure: @escaping ()->Void) {
        var finish:[Bool] = []
        for i in 0..<count {
            BirthdayEntity.execute.insert({ (flag) in
                finish.append(true)
                if finish.count == count {
                    finishClosure()
                }
            }) { (entity) in
                entity.birthdayId = Int64(i)
                entity.alert = true
                entity.name = "测试\(i)"
                entity.birthdayDay = 12
                entity.birthdayMonth = 10
            }
        }
    }

    func testMoreInsert() {
        
        let expec = self.expectation(description: "insert")
        
        BirthdayEntity.execute.deleteAll()
        let count = 100;
        insertAnyValue(count: count) {
            expec.fulfill()
        }
        
        self.waitForExpectations(timeout: 3) { (error) in
            if error != nil {
                XCTAssert(false, "\(String(describing: error))")
                return
            }
            let entitys = BirthdayEntity.execute.fetchAll("birthdayId", true)
            XCTAssertTrue(entitys.count == count, "插入数据总数不对\(entitys.count)")
            for i in 0..<count {
                let entity = entitys[i]
                XCTAssertTrue(entity.birthdayId == i, "第\(i)个实体\(entity.birthdayId)不对，顺序不对")
                XCTAssertEqual(entity.name, "测试\(i)", "第\(i)个实体名\(entity.name ?? "")不对，顺序不对")
            }
        }
    }
    
    func testMoreObtain() {
        let expec = self.expectation(description: "obtain")
        BirthdayEntity.execute.deleteAll()
        
        let count = 100
        insertAnyValue(count: count) {
            expec.fulfill()
        }
        self.waitForExpectations(timeout: 3) { (error) in
            
            let entitys = BirthdayEntity.execute.fetchAll("", true)
            XCTAssertTrue(entitys.count == count, "验证查实体方法，插入数据总数不对\(entitys.count)")
            let cou = BirthdayEntity.execute.fetchCount("")
            XCTAssertTrue(cou == count, "验证查数方法，插入数据总数不对")
            
            let objs = BirthdayEntity.execute.fetch("name='测试0'")
            let obj = objs.first!
            XCTAssertEqual(obj.name, "测试0", "查处的数据不对")
            
            let limitObjs = BirthdayEntity.execute.fetch(limit: 10, "birthdayId", true)
            for i in 0..<limitObjs.count {
                let limitObj = limitObjs[i]
                XCTAssertTrue(limitObj.birthdayId == i, "查出数据排序不对")
            }
            
        }
        
        BirthdayEntity.execute.deleteAll()
    }
    
    func testMoreChange() {
        let expec = self.expectation(description: "insert")
        BirthdayEntity.execute.insert({ (flag) in
            expec.fulfill()
        }) { (obj) in
            obj.birthdayId = 100;
        }
                
        self.waitForExpectations(timeout: 3) { (error) in
            let objs = BirthdayEntity.execute.fetch("birthdayId=100")
            let obj = objs.first!
            XCTAssertTrue(obj.birthdayId == 100, "数据插入存在问题")
        }
    }
    
    func testMoreDelete() {
        
        insertAnyValue(count: 100) {}
        
        let expecDelete = self.expectation(description: "delete")
        BirthdayEntity.execute.deleteAll { (flag) in
            expecDelete.fulfill()
        }
        
        self.waitForExpectations(timeout: 3) { (error) in
            let entitys = BirthdayEntity.execute.fetchAll("", true)
            XCTAssertTrue(entitys.count == 0, "实体删除失败 ， 还有数据\(entitys.count)个")
        }

    }
    
    func testSingleInsertSearch() {
        let expec = self.expectation(description: "insert")
        
        BirthdayEntity.execute.insert({ (flag) in
            expec.fulfill()
        }) { (entity) in
            entity.birthdayId = 1000;
        }
        
        self.waitForExpectations(timeout: 3) { (error) in
            let entity = BirthdayEntity.execute.fetch("birthdayId=\(1000)")
            guard let e = entity.first else {
                XCTAssert(false, "解析失败，entity中不存在值")
                return
            }
            XCTAssertTrue(e.birthdayId == 1000, "查找的值不正确")
        }
    }
    
    func testSingleInsertDelegateSearch() {
        let expec = self.expectation(description: "delete")
        BirthdayEntity.execute.deleteAll()

        BirthdayEntity.execute.insert({ (finish) in
            let entitys = BirthdayEntity.execute.fetch("birthdayId=\(1000)")
            let e = entitys[0]
            fearfulPrint("\(e)")
            XCTAssertTrue(e.birthdayId == 1000, "数据不对")
            
            e.execute.delete({ (flag) in
                expec.fulfill()
            })
            
        }) { (obj) in
            obj.birthdayId = 1000;
        }
        
        self.waitForExpectations(timeout: 3) { (error) in
            let entity = BirthdayEntity.execute.fetch("birthdayId=\(1000)")
            XCTAssertNil(entity.first, "数据不为空")
        }
    }
    

    func testPerformanceExample() {
        // This is an example of a performance test case.
        self.measure {
            // Put the code you want to measure the time of here.
        }
    }

}
